//
//  NYIPAViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/2/4.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYIPAViewController.h"

//views
#import "NYIPAHeadReusableView.h"
#import "NYIPAFooterReusableView.h"
#import "NYIPACollectionViewCell.h"

//model
#import "NYIAPModel.h"

//request
#import "NYNumRequest.h"
#import "PayManager.h"
#import "NYIAPValidationRequest.h"

@interface NYIPAViewController ()

@property (nonatomic, copy) NSString *numString;
@end

@implementation NYIPAViewController

#pragma mark - Life Cycle Methods
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
}
#pragma mark - Intial Methods
- (void)initCollectionView {
    [super initCollectionView];
    [self.collectionView registerClass:[NYIPAHeadReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[NYIPAHeadReusableView className]];
    [self.collectionView registerClass:[NYIPAFooterReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[NYIPAFooterReusableView className]];

    [self.collectionView setBackgroundColor:[UIColor whiteColor]];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.collectionView.allowsMultipleSelection = true;

}
- (void)configNimbus {
    [super configNimbus];
    [self.collectionViewActions forwardingTo:self.controllerView];

    @weakify(self);
    [self.collectionViewActions attachToClass:[NYIAPModel class] tapBlock:^BOOL(NYIAPModel *object, id target, NSIndexPath *indexPath) {
        
        @strongify(self);
        [NYTipClass showLoadingInView:self.view];
        [[PayManager manager] payItunes:object.iapID completion:^(NSArray * _Nullable content, NSError * _Nullable error, VerifyReceiptCompletion completion) {
            
            if (!error && content) {
                //向服务器验证
                NSString *timeSp = [NSString stringWithFormat:@"no%ld", (long)[[NSDate date] timeIntervalSince1970]];
                [[NYIAPValidationRequest requestWithParams:@{
                                                             kRequestReceipt : [content firstObject],
                                                             kRequestTransactionId : timeSp,
                                                             kRequestNum : object.num,
                                                             kRequestKeyUser_ID : NOT_NULL_STRING_DEFAULT_EMPTY(NYUserManager.user_id),
                                                             kRequestKeyLoginCode : NOT_NULL_STRING_DEFAULT_EMPTY(NYUserManager.logincode),
                                                             }] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof id  _Nonnull data) {
                    
                    [NYTipClass hideLoadingInView:self.view];
                    [NYTipClass showText:request.tipString inView:self.view];
                    [self loadData];
                } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                    [NYTipClass hideLoadingInView:self.view];
                    [NYTipClass showText:error.localizedDescription];                }];
            }
        }];
        return true;
    }];
}
#pragma mark - Network Methods
- (void)loadData {
    [self setLoading:YES animation:YES];
    [self configNimbus];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"NYIAPData" ofType:@"plist"];
    NSArray *arrayM = [NSArray arrayWithContentsOfFile:path];
    [self.collectionViewModel addObjectsFromArray:[NSArray modelArrayWithClass:[NYIAPModel class] json:arrayM]];
    [self.collectionView reloadData];
    

    [self setLoading:YES animation:YES];
    @weakify(self);
    [[NYNumRequest requestWithParams:nil] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NSDictionary * _Nonnull data) {
        
        @strongify(self);
        self.numString = [NSString stringWithFormat:@"%@",data[@"currency"]];
        [self.collectionView reloadData];
        [self setSuccess];
    } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
        [self setError:error];
    }];

}
#pragma mark - Target Methods

#pragma mark - Public Methods
- (void)configNavigationBar:(BaseNavigationBar *)navigationBar {
    [super configNavigationBar:navigationBar];
    self.title = @"充值牛币";
    
}
+ (NYViewControllerNavigationBarType)navigationBarType {
    
    return NYViewControllerNavigationBarTypeBlue;
}

#pragma mark - Private Method

#pragma mark - External Delegate

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (UICollectionReusableView *)collectionViewModel:(NICollectionViewModel *)collectionViewModel collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        NYIPAHeadReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:[NYIPAHeadReusableView className] forIndexPath:indexPath];
        [header setTheNumText:self.numString];
        return header;
    } else if (kind == UICollectionElementKindSectionFooter) {
        NYIPAFooterReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:[NYIPAFooterReusableView className] forIndexPath:indexPath];
        return footer;
    }
    return nil;
}

#pragma mark - Setter Getter Methods

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
